var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

function getAllChangeInfo(id){
    $.ajax({
        url:'https://'+ window.location.host +'/api/admin/acontract/customerExt/findChangeWarehouseInfoByPage.jhtml',
        cache: false, //禁用缓存
        type: "POST",
        dataType: "json",
        data: {
            customerCode: id,
            pageNum:1,
            pageSize:20
        },
        success: function(result){
            if(result.errorCode=="0"){
                goodsList(result);
                $('.M-box3').pagination({
                    pageCount: result.data.totalPage, //总页数
                    jump: true,
                    coping: true,
                    prevContent: '上页',
                    nextContent: '下页',
                    callback: function (api) {
                        $.ajax({
                            url: 'https://'+ window.location.host +'/api/admin/acontract/customerExt/findChangeWarehouseInfoByPage.jhtml',
                            cache: false, //禁用缓存
                            type: "POST",
                            dataType: "json",
                            data: {
                                customerCode: id,
                                pageNum:api.getCurrent(),
                                pageSize:20
                            },
                            success: function (result) {
                                if(result.errorCode=="0"){
                                    goodsList(result);
                                }else {
                                    layer.msg(result.errorMessage, {icon: 5,time:1000});
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                //alert(XMLHttpRequest);
                            }
                        });
                    }
                });
            }else {
                layer.msg(result.errorMessage, {icon: 5,time:1000});
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}


function goodsList(result) {
    if(dataSource.length>0){
        dataSource.splice(0, dataSource.length); //清空数组
    }
    if(result.data != null){
        $.each(result.data.result, function(i, field) {
            var rowData = new Array();
            rowData.push(field.id);
            rowData.push(field.originalValue);
            rowData.push(field.changeValue);
            rowData.push(field.changeUser);
            rowData.push(field.changeDate);
            rowData.push(field.remarks);
            dataSource.push(rowData);
        });
    }
    rangeDataTable(dataSource);
    function rangeDataTable(dataSource) {
        var dttable = $('#wayTable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#wayTable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            }
                /*{
                    "render": function(data, type, row, meta) {
                        if(row[1] == 10){
                            return '自有物流';
                        }else if (row[1] == 20) {
                            return '第三方物流';
                        }
                    },
                    "targets": 1
                },*/

            ]
        });
    };
}